//
//  JKOCRealmRelationModelTests.m
//  JKOCRealmTests
//
//  Created by 王冲 on 2019/2/27.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <Realm/Realm.h>
#import "Student.h"
#import "Dog.h"
@interface JKOCRealmRelationModelTests : XCTestCase

@end

@implementation JKOCRealmRelationModelTests

-(void)testHome{
    
    NSLog(@"%@",NSHomeDirectory());

}

#pragma mark 对一关系：多对一或一对一关系
- (void)testExample1{
    
    Student *student = [[Student alloc]init];
    student.studentId = 1;
    student.studentName = @"小明";
    
    Dog *dog = [[Dog alloc]init];
    dog.dogId = 1;
    dog.dogName = @"旺财";
    
    // student.dog = dog;
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    
    [realm transactionWithBlock:^{
       
        [realm addObject:student];
    }];
    
}
    
#pragma mark 对多关系
- (void)testExample2{
    
    Student *student = [[Student alloc]init];
    student.studentId = 1;
    student.studentName = @"小明";
    
    Dog *dog1 = [[Dog alloc]init];
    dog1.dogId = 1;
    dog1.dogName = @"旺财";
    
    Dog *dog2 = [[Dog alloc]init];
    dog2.dogId = 2;
    dog2.dogName = @"阿福";

    [student.dogs addObject:dog1];
    [student.dogs addObject:dog2];
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    
    [realm transactionWithBlock:^{
        
        [realm addObject:student];
    }];
}
    
#pragma mark 反向关系：关系是单向的，
- (void)testExample3{
    
    RLMResults *results = [Student allObjects];
    
    Student *student = results.firstObject;
    
    Dog *dog = student.dogs.firstObject;
    
    NSLog(@"owners=%@",dog.owners);
    
}

@end
